package com.jsp.controller; /**
 * @author wzm
 * @create 2023-11-09 14:11
 * project_name:web2310
 * @Description
 */

import com.jsp.pojo.Student;
import com.jsp.service.StudentService;
import com.jsp.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {
    private StudentService studentService = new StudentServiceImpl();

    //查询全部学生方法
    protected void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //模拟从数据库查询全部数据
        //准备数据
     /*   List<Student> list = new ArrayList<>();
        list.add(new Student(1,"小明",18,'男',"云南昆明",1));
        list.add(new Student(2,"小明1",19,'男',"云南昆明",1));
        list.add(new Student(3,"小明2",20,'男',"云南昆明",1));
        list.add(new Student(4,"小明3",201,'男',"云南昆明",1));*/

        //1.设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2.调用Sercvice查询数据
        List<Student> students = studentService.FindByAll();

        //域对象
       // request.setAttribute("list",students);
        //session 会话对象  一次会话中有效
        request.getSession().setAttribute("list",students);

        //application级别的域对象
       // request.getServletContext().setAttribute("list",students);

        //转发到显示数据的页面
        //request.getRequestDispatcher("/student_list.jsp").forward(request,response);

        //重定向
        response.sendRedirect("/web07/student_list.jsp");


    }



    //新增学生的方法
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    //删除的方法
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        //获取参数
        Integer id = Integer.parseInt(request.getParameter("id")) ;

        //调用service层删除方法
       boolean flag = studentService.removeById(id);

       //判断结果
        if(flag){
            //删除成功 跳转查询数据 增删改用重定向 防止重复提交相同路径造成系统问题
            response.sendRedirect("/web07/student/getAll");
        }else{
            //失败
            response.getWriter().write("删除失败");
        }




    }


}
